import os
import numpy as np
import pandas as pd
import json
import toml
import cv2
import random
from pathlib import Path
from shutil import move, rmtree, copyfile
from collections import Counter
from glob import glob
import cv2
import math
import glob


path = r'/data2/autorepair/ruanzhifeng/autorepair_t7_10/t7/T7002'
df = pd.read_csv(r'/data2/autorepair/ruanzhifeng/autorepair_t7_10/t7/T7002_2.csv', index_col=False)

# path = r'F:\TCL\AutoRepair\data\t7\data_online\T7001'
# df = pd.read_csv(r'F:\TCL\AutoRepair\data\t7\data_online\T7001_2.csv', index_col=False)

c = Counter(df['img_name'])

process_res = {}
for img_name, t in c.items():
    if t != 2:
        continue
    tmp_df = df[df['img_name']==img_name].reset_index(drop=True)
    folder1 = tmp_df['folder'].iloc[0]
    folder2 = tmp_df['folder'].iloc[1]
    code1 = tmp_df['code'].iloc[0]
    code2 = tmp_df['code'].iloc[1]

    if not (folder1 in ['feedback', 'feedback_no_json'] and folder2 in ['feedback', 'feedback_no_json']):
        continue

    if code1 != code2:
        lists = [0, 1]
    else:
        if folder1 == 'feedback_no_json':
            lists = [0]
        else:
            lists = [1]
    
    for i in lists:
        os.remove(os.path.join(path, tmp_df['folder'].iloc[i], tmp_df['code'].iloc[i], img_name))
        label_name = Path(img_name).with_suffix('.json')
        label_path = os.path.join(path, tmp_df['folder'].iloc[i], tmp_df['code'].iloc[i], label_name)
        if os.path.exists(label_path):
            os.remove(label_path)
        else:
            label_name = Path(img_name).with_suffix('.png')
            label_path = os.path.join(path, tmp_df['folder'].iloc[i], tmp_df['code'].iloc[i], label_name)
            if os.path.exists(label_path):
                os.remove(label_path)
    process_res[img_name] = True


# c = Counter(df['img_name'])
# new_c = {}
# for key, val in c.items():
#     if val > 1:
#         new_c[key] = val

# process_res = {}
# for img_name, t in new_c.items():
#     if t != 2:
#         continue
#     tmp_df = df[df['img_name']==img_name].reset_index(drop=True)
#     if len(set(tmp_df['folder'])) != 1:
#         continue
    
#     folder = tmp_df['folder'].iloc[0]
#     for index, row in tmp_df.iterrows():
#         os.remove(os.path.join(path, folder, row['code'], img_name))
#         label_name = Path(img_name).with_suffix('.json')
#         label_path = os.path.join(path, folder, row['code'], label_name)
#         if os.path.exists(label_path):
#             os.remove(label_path)
#         else:
#             label_name = Path(img_name).with_suffix('.png')
#             label_path = os.path.join(path, folder, row['code'], label_name)
#             if os.path.exists(label_path):
#                 os.remove(label_path)
        
#     process_res[img_name] = True

# # df['processed'] = df['img_name'].apply(lambda x: ((x in process_res) and process_res[x]))
# # df = df[df['processed']!=True]
# # df.to_csv(os.path.join(path, 'T6006_2.csv'), index=False)